#include "blackhole.h"

Blackhole::Blackhole(Renderer *render, Logic *lgc, Sound *sound, int x, int y)
{
	rnd = render;
	logic = lgc;
	snd = sound;
	image = rnd->loadImage("Images//Blackhole.png");
	if (image==NULL)
		throw "Failed to load image";
	else
		texId = rnd->createTexture(image);

	position.w = image->w;
	position.h = image->h;
	position.x = x;
	position.y = y;
	angle = 0.0f;
	radius = 25;

	centerX = position.x + (position.w/2);
	centerY = position.y + (position.h/2);

	drawPos = new SDL_Rect();
	drawPos->w=image->w;
	drawPos->h=image->h;
	active = false;
}
Blackhole::~Blackhole()
{
	SDL_FreeSurface(image);

	delete drawPos;
}
void Blackhole::show(int mapX, int mapY) 
{
	if(active)
	{
		drawPos->x=position.x-mapX;
		drawPos->y=position.y-mapY;
		rnd->drawSurface(drawPos, angle, texId);
	}
}
void Blackhole::dragPlayer(Fighter *player)
{
	if(player->getHP() > 0)
	{
		if(logic->checkCollide(this, player))
		{
			int xdrag = 0;
			int ydrag = 0;
			int pcX = (player->getRect()->x + (player->getRect()->w/2));
			int pcY = (player->getRect()->y + (player->getRect()->h/2));

			if(pcX > (centerX+radius) || pcX < (centerX-radius))
			{
				if(pcX > centerX)
					xdrag = -force;
				else
					xdrag = force;
			}
			if(pcY > (centerY+radius) || pcY < (centerY-radius))
			{
				if(pcY > centerY)
					ydrag = -force;
				else
					ydrag = force;
			}

			if(xdrag == 0 && ydrag == 0)
			{
				player->setHP(0);
				snd->playSound(SND_ZIP, 0);
			}
			else
				player->setPos((player->getRect()->x + xdrag), (player->getRect()->y + ydrag));
		}
	}
}